package com.kyzh.core.activities

import android.os.Bundle
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.gushenge.core.base.activity.BaseActivity
import com.gushenge.core.beans.Notice
import com.gushenge.core.requests.UserRequest
import com.kyzh.core.R
import com.kyzh.core.databinding.ItemNoticeBinding
import com.kyzh.core.dialog.shouListWindow
import com.kyzh.core.utils.dip
import com.kyzh.core.utils.startActivity
import com.gushenge.core.toast
import kotlinx.android.synthetic.main.activity_notice.*

class NoticeActivity : BaseActivity() {

    private val beans = arrayListOf<Notice>()
    private val adapter = Adapter(R.layout.item_notice,beans)
    private var p =1
    private var max =2
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_notice)
        initView()
    }

    private fun initView() {
        tvHistory.setOnClickListener {
            UserRequest.noticeRead(0){
                for (i in beans){
                    i.read=1
                }
                adapter.notifyDataSetChanged()
            }
        }
        rvList.adapter = adapter
        refresh()
        adapter.loadMoreModule.isEnableLoadMore=true
        adapter.loadMoreModule.isEnableLoadMoreIfNotFullPage=true
        adapter.loadMoreModule.setOnLoadMoreListener {
            if (max<p){
                adapter.loadMoreModule.loadMoreEnd()
            }else{
                getData()
                adapter.loadMoreModule.loadMoreComplete()
            }
        }
        root.setOnRefreshListener {
            refresh()
            root.isRefreshing = false
        }
        adapter.setOnItemLongClickListener { adapter, view, position ->
            val list = arrayListOf(getString(R.string.delete))
            if (beans[position].read==0){
                    list.add(getString(R.string.markRead))
            }
            val window = shouListWindow(list){
                when(this){
                    0-> {
                        UserRequest.noticeDelete(beans[position].id){
                            beans.removeAt(position)
                            adapter.notifyDataSetChanged()
                        }
                    }
                    1-> {
                        UserRequest.noticeRead(beans[position].id){
                            beans[position].read=1
                            adapter.notifyDataSetChanged()
                        }
                    }
                }
            }

            window.showAsDropDown(
                    view,
                    (view.width - window.contentView.measuredWidth) / 2,
                    dip(-10)
            )
            return@setOnItemLongClickListener true
        }
    }


    private inner class Adapter(layout:Int,beans:ArrayList<Notice>):BaseQuickAdapter<Notice,BaseDataBindingHolder<ItemNoticeBinding>>(layout,beans), LoadMoreModule {
        override fun convert(holder: BaseDataBindingHolder<ItemNoticeBinding>, item: Notice) {
            holder.dataBinding?.data = item
            holder.itemView.setOnClickListener {
                UserRequest.noticeRead(item.id){
                    item.read=1
                    adapter.notifyDataSetChanged()
                }
                startActivity<NoticeDetailActivity>("data" to item)
            }
        }

    }



    fun getData(){
        UserRequest.notice(p){ p, max_p, data->
            this.p = p
            this.max = max_p
            beans.addAll(data)
            adapter.notifyDataSetChanged()
        }
    }
    fun refresh(){
        UserRequest.notice(1){ p, max_p, data->
            this.p = p
            this.max = max_p
            beans.clear()
            beans.addAll(data)
            adapter.notifyDataSetChanged()
            if (data.isEmpty()){
                getString(R.string.noNotice).toast()
            }
        }
    }
}